var assert = require('assert');
var should = require('should');
var util = require('util');
var _ = require('lodash');
var battleRecordModel = require('../../app/models/battleRecordModel');
var config = require('../config');
var atkData = require('../../app/libs/war/atkData');
var battleData = require('../../app/libs/war/battleData');
var fightRewardUtil = require('../../app/util/fightRewardUtil');

suite('测试battleRecordModel', function () {
    var battle_record_model;

    suite('添加一个战斗记录', function () {
        test('添加战斗记录成功', function (done) {
            atkData.getData(config.role_id, function (err, atk_data) {
                battleData.getData(config.role_id, function (err, def_data) {
                    battleRecordModel.addRecord(config.role_id, config.role_id, atk_data, def_data, function (err, model) {
                        battle_record_model = model;

                        // 验证所有的比例加起来是100
                        var total_per = 0;
                        _.each(battle_record_model.def.build_list, function (build_item) {
                            total_per += build_item.per;
                        });

                        // 验证抽奖
                        battle_record_model.canLucky().should.equal(false);
                        battle_record_model.star = 1;
                        for (var i = 0; i < 3; i++) {
                            if (battle_record_model.getSuccessLuckNum() == 3) {
                                break;
                            }

                            var reward = battle_record_model.lucky(i);
                            if (!reward || reward.reward_type == 0) {
                                battle_record_model.canLucky().should.equal(false);
                                battle_record_model.retryLucky();
                                battle_record_model.canLucky().should.equal(true);
                            }
                        };

                        battle_record_model.getDefRoleId();
                        battle_record_model.getDefName();
                        battle_record_model.getDefBuildList();
                        battle_record_model.setStar(1);
                        battle_record_model.setCup(1);
                        battle_record_model.setEarnGold(1);
                        battle_record_model.setEarnExp(1);
                        battle_record_model.getEarnExp();
                        battle_record_model.getProgress();
                        battle_record_model.getRewardList();
                        battle_record_model.getRetryNum();
                        battle_record_model.addReliveNum();
                        battle_record_model.addReliveDiamond(1);
                        battle_record_model.toJSON();

                        var damage_build_list = [];
                        _.each(battle_record_model.getDefBuildList(), function (def_build) {
                            damage_build_list.push({
                                build_id: def_build.build_id,
                                hp: def_build.attr.hp
                            });
                        });
                        fightRewardUtil.getEconomyBuildGold(
                            battle_record_model, damage_build_list
                        );
                        fightRewardUtil.getStar(battle_record_model, damage_build_list);
                        fightRewardUtil.getDestroyBuildExp(battle_record_model, damage_build_list);

                        _.each(battle_record_model.getDefBuildList(), function (def_build) {
                            damage_build_list.push({
                                build_id: def_build.build_id,
                                hp: def_build.attr.hp + 9999
                            });
                        });
                        fightRewardUtil.getEconomyBuildGold(
                            battle_record_model, damage_build_list
                        );

                        _.each(battle_record_model.getDefBuildList(), function (def_build) {
                            damage_build_list.push({
                                build_id: def_build.build_id,
                                hp: 0
                            });
                        });
                        fightRewardUtil.getEconomyBuildGold(
                            battle_record_model, damage_build_list
                        );

                        done();
                    });
                });
            });
        });
    });

    suite('获取一个战斗记录', function () {
        test('获取战斗记录成功', function (done) {
            battleRecordModel.getRecordByAtkRoleId(config.role_id,
                battle_record_model.getRecordId(),
                function (err, model) {
                    battle_record_model = model;
                    done();
                });
        });
    });

    suite('getRecordListByDefRoleId', function () {
        test('获取掠夺列表', function (done) {
            battleRecordModel.getRecordListByDefRoleId(config.role_id,
                function (err, record_list) {
                    done();
                });
        });
    });
});
